<?php

namespace app\common\model;


use think\Model;

class MemberAppealModel extends Model
{
    protected $name = 'member_appeal';

    // 自动写入时间戳字段
    protected $autoWriteTimestamp = 'int';

    // 定义时间戳字段名
    protected $createTime = 'createtime';
    protected $updateTime = false;
    protected $deleteTime = false;



    /**
     * 发布诉求
     * @ApiMethod   (POST)
     * @param int $user_id
     * @param string $name 联系人
     * @param string $mobile 联系电话
     * @param string $content 诉求内容
     * @param string $images 诉求图片
     * @param int $is_open 公开:0=否,1=是
     * @return array
     */
    public function issueMemberAppeal($user_id, $name, $mobile, $content, $images, $is_open): array
    {
        try {
            $mid = 0;
            $member = model('app\admin\model\wdsxh\Member')->where('uid',$user_id)->find();
            if($member && $member['status'] == 1){
                $mid = $member['id'];
            }
            $this->create([
                'uid' => $user_id,
                'mid' => $mid,
                'name' => $name,
                'mobile' => $mobile,
                'content' => $content,
                'images' => $images,
                'is_open' => $is_open,
                'createtime' => time(),
            ]);

            return [
                'code' => 200,
                'msg' => '发布成功',
                'data' => [],
            ];
        }catch (\Exception $e) {
            return [
                'code' => $e->getCode(),
                'msg' => $e->getMessage(),
                'data' => []
            ];
        }
    }


    /**
     * 获取分页列表
     * @param string $mold open=公开,my=我的
     * @param int $user_id
     * @param int $page
     * @param int $limit 数据条数
     * @return array
     */
    public function getPageList(string $mold, int $user_id, int $page = 1, int $limit = 5): array
    {
        try {
            $where = [];
            $field = ['id','content','images','reply','star','is_open','evaluate_status','status','createtime'];
            $order = [
                'createtime' => 'desc',
            ];
            if($mold == 'open') {
                $where['is_open'] = 1;
                $where['status'] = 1;
            }
            if($mold == 'my') {
                $where['uid'] = $user_id;
            }
            $list = $this
                ->where($where)
                ->field($field)
                ->order($order)
                ->paginate($limit, false, ['page' => $page]);
            foreach ($list as $row){
                $row->createtime = datetime($row->createtime,'Y-m-d H:i');
                $row->images = wdsxh_full_url($row->images,true);
            }
            return [
                'code' => 200,
                'msg' => '请求成功',
                'data' => $list
            ];
        }catch (\Exception $e) {
            return [
                'code' => $e->getCode(),
                'msg' => $e->getMessage(),
                'data' => []
            ];
        }
    }


    /**
     * 评价
     * @ApiMethod   (POST)
     * @param int $id
     * @param int $user_id
     * @param int|string $star
     * @return array
     */
    public function evaluateMemberAppeal(int $id, int $user_id, $star): array
    {
        try {
            $data = $this->get($id);
            if($data) {
                $data->evaluate_status = 1;
                $data->star = $star;
                $data->evaluate_time = time();
                $data->save();
            }

            return [
                'code' => 200,
                'msg' => '评价成功',
                'data' => [],
            ];
        }catch (\Exception $e) {
            return [
                'code' => $e->getCode(),
                'msg' => $e->getMessage(),
                'data' => []
            ];
        }
    }


}